package com.zhou.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.config.Result;
import com.zhou.entity.TheoQues;
import com.zhou.entity.TheoQuesAns;
import com.zhou.enums.IsRightEnum;
import com.zhou.mapper.TheoQuesAnsMapper;
import com.zhou.service.TheoQuesAnsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.zhou.enums.IsRightEnum.RIGHT;

/**
 * <p>
 * 题目信息表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2023-03-02
 */
@RestController
@RequestMapping("/question")
@Api(tags = "题目答案表接口信息")
public class TheoQuesAnsController {

    @Autowired
    private TheoQuesAnsMapper theoQuesAnsMapper;

    @Autowired
    private TheoQuesAnsService theoQuesAnsService;

    private static final Integer SIZE = 10;

    @ApiOperation(value = "以列表形式返回所有题目的答案")
    @GetMapping("/findAllAns")
    public List<TheoQuesAns> findAllAns(){
        return this.theoQuesAnsMapper.selectList(null);
    }

    @ApiOperation(value="呈现四个选项(contend)")
    @GetMapping("/options")
    public List<String>getOptionsByQuesId(@RequestParam("quesId")Integer quesId){
        List<TheoQuesAns>optinsList=theoQuesAnsService.lambdaQuery().eq(TheoQuesAns::getQuesId,quesId).list();
        List<String>options=new ArrayList<>();
        for(TheoQuesAns option:optinsList){
            options.add(option.getContend());
        }
        return options;
    }

    @ApiOperation(value = "增加一条答案信息，无返回值")
    @PutMapping("/addAns")
    public void addAns(@RequestBody TheoQuesAns theoQuesAns){
        theoQuesAnsMapper.insert(theoQuesAns);
    }

    @ApiOperation(value = "修改单条的题目答案信息")
    @PostMapping("/updateAns")
    public void updateAns(@RequestBody TheoQuesAns theoQuesAns){
        theoQuesAnsService.saveOrUpdate(theoQuesAns);
    }



    /**
     * 判断前端传回的选项数组是否与数据库中四个选项数组相同
     * @param quesId 题目编号
     * @param selectedOptions 前端传回的选项数组
     * @return 如果相同，返回1；否则返回0
     */
    @ApiOperation(value="根据is_right拼接数组进行判断")
    @PostMapping("/check")
    public int checkAnswer(@RequestParam("quesId") Integer quesId,  @RequestBody  int[] selectedOptions) {
        // 查询数据库中的四个选项
        List<TheoQuesAns> optionList = theoQuesAnsService.lambdaQuery().eq(TheoQuesAns::getQuesId, quesId).orderByAsc(TheoQuesAns::getId).list();
        // 获取四个选项的is_right值，拼接为一个数组
        int[] isRightArray = new int[optionList.size()];
        for (int i = 0; i < optionList.size(); i++) {
            IsRightEnum isRight = optionList.get(i).getIsRight();
            if (isRight == null) {
                throw new RuntimeException("选项 " + (i + 1) + " 的is_right值为空");
            }
            isRightArray[i] = isRight.getValue();
        }
        // 将拼接好的is_right数组值和前端传回的选项数组进行对比，判断是否相同
        if (Arrays.equals(selectedOptions, isRightArray)) {
            System.out.println(optionList.size());
            for (int i = 0; i < optionList.size();i++)
            {
                System.out.print(isRightArray[i]);
            }
            return 1;
        } else {
            return 0;
        }
    }


    @ApiOperation(value = "判断题答案检查")
    //前端直接传输“正确”或者“错误”，后端和正确答案进行判断
    @PostMapping ("/checkJudge")
    public int checkJudge(@RequestBody Map<String, Object> map){
        Integer quesId = (Integer) map.get("quesId");
        String judge = (String) map.get("judge");
        QueryWrapper<TheoQuesAns> theoQuesAnsQueryWrapper = new QueryWrapper<>();
        theoQuesAnsQueryWrapper.eq("ques_id",quesId);
        String value = theoQuesAnsMapper.selectOne(theoQuesAnsQueryWrapper).getIsRight().getDisplayName();
        //如果用户提交的答案正确返回1，错误则返回0
        return (value.equals(judge)) ? 1 : 0;
    }


}


